package CreationMode.single.demo04;

public class Single {
    private volatile static Single single;

    private volatile static Boolean isFirstCreate = true ;

    // 防止放射对
    private Single() {
        synchronized (Single.class){

            if(!isFirstCreate){
                throw new RuntimeException("不能创建多个对象");
            }

            isFirstCreate = false;
        }
    }


    public static Single getSingle() {

        // 第一次检查如果没有实列化 就抢占资源
        if(single == null){
            synchronized(Single.class){

                // 第二次检查 用于防止多次创建实列
               if(single == null){
                   single = new Single();
               }
            }
        }
        return single;
    }


    // 反序列化自动调用 防止序列化
    public Object readResolve(){
         return single;
    }
}
